package com.prs.crm.context;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.prs.crm.domain.sms.SmsSend;



public class ToolKits {
	public static final int SMS_MAX_LENGTH = 68;
	
	public static String currentDate() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date d = new Date();
		String str = df.format(d);

		return str;
	}
	
	//得到当前时间
	public static java.util.Date currentFullTime() 
	{
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String datestr = sdf.format(java.util.Calendar.getInstance().getTime());
			return sdf.parse(datestr);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}
	}

	public static String getCurrentDateDigi() {
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		Date d = new Date();
		String str = df.format(d);

		return str;
	}

	// 计算两个时间点之间的天数
	public static int getDaysBetween(Calendar d1, Calendar d2) {
		if (d1.after(d2)) {
			java.util.Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
		int y2 = d2.get(Calendar.YEAR);
		if (d1.get(Calendar.YEAR) != y2) {
			d1 = (Calendar) d1.clone();
			do {
				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数
				d1.add(Calendar.YEAR, 1);
			} while (d1.get(Calendar.YEAR) != y2);
		}
		return days;
	}
	
	public static Collection<Integer> convertStringAryToIntAry(String[] ary)
	{
		if (ary!=null)
		{
			Collection<Integer> cols = new ArrayList<Integer>();
			for (String str:ary)
			{
				if (str.trim().length()>0)
				{
					cols.add(Integer.parseInt(str.trim()));
				}
			}
			return cols;
		}
		else
		{
			return null;
		}
	}
	
	public static Collection<Integer> convertToIntAry(Integer[] ary) {
		if (ary!=null)
		{
			Collection<Integer> cols = new ArrayList<Integer>();
			for (Integer str:ary)
			{
				cols.add(str);
			}
			return cols;
		}
		else
		{
			return null;
		}
	}
	
	public static Integer[] convertStringAryToIntAry(String strarg,String split)
	{
		String[] ary = strarg.split(split);
		if (ary!=null && ary.length>0)
		{
			Integer[] cols = new Integer[ary.length];
			int i=0;
			for (String str:ary)
			{
				if (str.trim().length()>0)
				{
					cols[i++] = Integer.parseInt(str.trim());
				}
				else
				{
					cols[i++]=0;
				}
			}
			return cols;
		}
		else
		{
			return null;
		}
	}
	
	public static String constructInSql(Integer[] idary)
	{
		StringBuilder sb = new StringBuilder("-1");
		if (idary!=null)
		{
			for (int i = 0; i < idary.length; i++) {
				sb.append("," + idary[i]);
			}
		}
		return sb.toString();
	}
	
	/**
	 * 根据手机号码返回所属运营商
	 * @param telephoneNo
	 * @return
	 */
	public static String getOperator(String telephoneNo) {
		String flag = "";
		Collection<String> mobiles = new ArrayList<String>();
		mobiles.add("134");
		mobiles.add("135");
		mobiles.add("136");
		mobiles.add("137");
		mobiles.add("138");
		mobiles.add("139");
		mobiles.add("150");
		mobiles.add("151");
		mobiles.add("152");
		mobiles.add("158");
		mobiles.add("159");
		mobiles.add("188");
		
		Collection<String> unicoms = new ArrayList<String>();
		unicoms.add("130");
		unicoms.add("131");
		unicoms.add("132");
		unicoms.add("156");
		unicoms.add("186");
		
		Collection<String> telecoms = new ArrayList<String>();
		telecoms.add("133");
		telecoms.add("153");
		telecoms.add("189");
		
		String no = telephoneNo.substring(0, 3);
		if(mobiles.contains(no)) {
			flag = Constrains.OPERATOR_CHINAMOBILE;
		}else if(unicoms.contains(no)) {
			flag = Constrains.OPERATOR_UNICOM;
		}else if(telecoms.contains(no)) {
			flag = Constrains.OPERATOR_TELECOM;
		}
		return flag;
	}
	
	/**
	 * 判断是否为手机号码
	 * @param phoneNo
	 * @return
	 */
	public static boolean isTelephoneNo(String phoneNo) {
		boolean flag = false;
		if(phoneNo.length() != 11) {
			flag = false;
		}else if(isInteger(phoneNo)){
			flag = true;
		}
		
		return flag;
	}
	
	/**
	 * 判断是否为整数型数字
	 * @param str
	 * @return
	 */
	public static boolean isInteger(String str) {
        int begin = 0;
        if (str == null || str.trim().equals("")) {
            return false;
        }
        str = str.trim();
        if (str.startsWith("+") || str.startsWith("-")) {
            if (str.length() == 1) {
                // "+" "-"
                return false;
            }
            begin = 1;
        }
        for (int i = begin; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
	
	 /**
     * 判断是否为数字
     * "33" "+33" "033.30" "-.33" ".33" " 33." " 000.000 "
     * @param str String
     * @return boolean
     */
   public static boolean isNumeric(String str) {
       int begin = 0;
       boolean once = true;
       if (str == null || str.trim().equals("")) {
           return false;
       }
       str = str.trim();
       if (str.startsWith("+") || str.startsWith("-")) {
           if (str.length() == 1) {
               // "+" "-"
               return false;
           }
           begin = 1;
       }
       for (int i = begin; i < str.length(); i++) {
           if (!Character.isDigit(str.charAt(i))) {
               if (str.charAt(i) == '.' && once) {
                   // '.' can only once
                   once = false;
               }
               else {
                   return false;
               }
           }
       }
       if (str.length() == (begin + 1) && !once) {
           // "." "+." "-."
           return false;
       }
       return true;
   }

	
	public static List<SmsSend> pageSms(SmsSend send) {
		String msg = send.getMessage();
		List<SmsSend> csl = new ArrayList<SmsSend>();
		if (msg.length() > SMS_MAX_LENGTH) {
			int f = 0;
			if (msg.length() % SMS_MAX_LENGTH == 0) {
				f = msg.length() / SMS_MAX_LENGTH;
			} else {
				f = msg.length() / SMS_MAX_LENGTH + 1;
			}
			for (int i = 0; i < f; i++) {
				SmsSend sub = new SmsSend();
				sub.setClientName(send.getClientName());
				sub.setBaseSpNo(send.getBaseSpNo());
				sub.setExtSpNo(send.getExtSpNo());
				sub.setMessage(send.getMessage());
				sub.setServiceId(send.getServiceId());
				sub.setTelephoneNo(send.getTelephoneNo());
				sub.setState(send.getState());
				sub.setWapUrl(send.getWapUrl());

				if (((i + 1) * SMS_MAX_LENGTH) < sub.getMessage().length()) {
					sub.setMessage(sub.getMessage().substring(
							SMS_MAX_LENGTH * i, (i + 1) * SMS_MAX_LENGTH));
				} else {
					sub.setMessage(sub.getMessage().substring(
							SMS_MAX_LENGTH * i, sub.getMessage().length()));
				}
				if (sub.getMessage() != null) {
					csl.add(sub);
				}
			}
		} else {
			csl.add(send);
		}

		return csl;
	}
}
